package xpn.business.fund_return.entity.return_funds_report;

import org.springframework.context.ApplicationContextException;
import org.springframework.stereotype.Service;
import xpn.business.fund_return.entity.frcase.FrCase;
import xpn.business.fund_return.entity.frcase.FrCaseDao;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class ReturnFundsReportService {
    @Resource
    private ReturnFundsReportDao returnFundsReportDao;

//    @Resource
//    private FrCaseService frCaseService;

    @Resource
    private FrCaseDao frCaseDao;

    public ReturnFundsReport save(ReturnFundsReport returnFundsReport) {
        if(Objects.isNull(returnFundsReport.getCaseId())) {
            throw new ApplicationContextException("案件id为空！");
        }
        if (Objects.isNull(returnFundsReport.getId())) {
            ReturnFundsReport exist = returnFundsReportDao.findAllByCaseId(returnFundsReport.getCaseId());
            if (Objects.nonNull(exist)) {
                throw new ApplicationContextException("该表单已存在，请勿重复创建");
            }
        }
        FrCase frCase = frCaseDao.getOne(returnFundsReport.getCaseId());
        frCase.setReturnReason(returnFundsReport.getReturnReason());
        frCaseDao.save(frCase);
//        frCaseService.changeWriteStatus(returnFundsReport.getCaseId(), CaseWriteStatus.Report);
        return returnFundsReportDao.save(returnFundsReport);
    }
}
